package com.ruoyi.web.controller.dm;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.dm.domain.AtmWindFieldParam;
import com.ruoyi.dm.domain.DataCollectionParam;
import com.ruoyi.dm.domain.DataManagementParam;
import com.ruoyi.dm.service.DataCollectionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/dm/dataCollect")
@Api(value = "DataCollectionController", tags = "数据管理")
public class DataCollectionController extends BaseController {

    @Resource
    private DataCollectionService dataCollectionService;



    //@PreAuthorize("@ss.hasPermi('dm:dataCollect:import')")
    @ApiOperation(value = "数据导入", notes = "数据导入")
    @PostMapping("/import")
    @ApiImplicitParam(value = "上传文件",name = "files",dataTypeClass = File.class,required = true)
    public AjaxResult add(@RequestPart @RequestParam(value = "files", required = false) MultipartFile[] files,DataCollectionParam param) {
        return dataCollectionService.add(files, param);
    }



    //@PreAuthorize("@ss.hasPermi('dm:dataCollect:getCatalogList')")
    @ApiOperation(value = "获取数据目录", notes = "获取数据目录")
    @GetMapping("/getCatalogList/{parentId}")
    public AjaxResult getCatalogList(@ApiParam(value = "父ID", name = "parentId", required = true)@PathVariable(value = "parentId") Long parentId) {
        return dataCollectionService.getCatalogList(parentId);
    }

    //@PreAuthorize("@ss.hasPermi('dm:dataCollect:getSourceList')")
    @ApiOperation(value = "获取数据来源列表", notes = "获取数据来源列表")
    @GetMapping("/getSourceList")
    public AjaxResult getSourceList(DataManagementParam param) {
        return dataCollectionService.getSourceList(param);
    }

    //@PreAuthorize("@ss.hasPermi('dm:dataCollect:list')")
    @ApiOperation(value = "获取数据列表", notes = "获取数据列表")
    @GetMapping("/list")
    public AjaxResult list(DataCollectionParam param) {
        return dataCollectionService.selectList(param);
    }

    //@PreAuthorize("@ss.hasPermi('dm:dataCollect:download')")
    @ApiOperation(value = "文件下载", notes = "下载文件")
    @PostMapping(value = "download")
    public void download(@RequestBody List<DataCollectionParam> list,
                           HttpServletResponse response) throws Exception {
        dataCollectionService.download(list, response);
    }
    //@PreAuthorize("@ss.hasPermi('dm:dataCollect:downloadTemplate')")
    @ApiOperation(value = "模版下载", notes = "模版下载")
    @GetMapping("/downloadTemplate/{dataId}")
    public void downloadTemplate(@ApiParam(value = "资料ID", name = "dataId", required = true)@PathVariable(value = "dataId") Long dataId,HttpServletResponse response){
        dataCollectionService.downloadTemplate(dataId,response);
    }
    /**
     * 修改资料
     */
    //@PreAuthorize("@ss.hasPermi('dm:dataCollect:edit')")
    @Log(title = "修改数据", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改数据")
    public AjaxResult edit(DataCollectionParam param)
    {
        return dataCollectionService.edit(param);
    }

    /**
     * 删除资料
     */
    //@PreAuthorize("@ss.hasPermi('dm:dataCollect:remove')")
    @Log(title = "删除数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove")
    @ApiOperation("删除数据")
    public AjaxResult remove(@RequestBody List<DataCollectionParam> params)
    {
        return dataCollectionService.delete(params);
    }

    /**
     * 删除磁盘文件
     */
    //@PreAuthorize("@ss.hasPermi('dm:dataCollect:deleteDiskFile')")
    @Log(title = "删除磁盘文件", businessType = BusinessType.DELETE)
    @DeleteMapping("/deleteDiskFile")
    @ApiOperation("删除磁盘文件")
    public AjaxResult deleteDiskFile(@ApiParam(value = "文件路径",name="filePath" ,required = true) @RequestBody Map<String, Object> map)
    {
        return dataCollectionService.deleteDiskFile(map);
    }

    @ApiOperation("多线程调用python测试")
    @GetMapping("/test")
    public AjaxResult test(@ApiParam(value = "数量", name = "nums")@RequestParam("nums") List<String> nums,@ApiParam(value = "标识", name = "id")@RequestParam("id") String id)
    {
        return dataCollectionService.test(nums,id);
    }
    @GetMapping(path = "subscribe", produces = {MediaType.TEXT_EVENT_STREAM_VALUE})
    public SseEmitter push(String id,String flag) {

        return dataCollectionService.push(id, flag);
    }
    @ApiOperation("数据入库测试")
    @PostMapping("/importData")
    public AjaxResult importData(@ApiParam(value = "路径", name = "path") @RequestParam("path") String path,@RequestParam("nums") Integer nums)
    {
        return dataCollectionService.importData(path,nums);
    }
    @ApiOperation("地磁数据入库测试")
    @PostMapping("/importDc")
    public AjaxResult importDc(@ApiParam(value = "路径", name = "path") @RequestParam("path") String path,@RequestParam("nums") Integer nums)
    {
        return dataCollectionService.importDc(path,nums);
    }
    @ApiOperation("大气风场数据导入")
    @PostMapping("/importAtmWindField")
    //@PreAuthorize("@ss.hasPermi('dm:dataCollect:importAtmWindField')")
    public AjaxResult importAtmWindField(@ApiParam(value = "文件", name = "files") @RequestParam(value = "files", required = false) MultipartFile[] files,
                                         AtmWindFieldParam param, @ApiParam(value = "站点文件", name = "stationFile") @RequestParam(value = "stationFile", required = false) MultipartFile stationFile)
    {
        return dataCollectionService.importAtmWindField(files,param,stationFile);
    }

}
